﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.ComponentModel.Design
    <DesignerSerializer(GetType(ThemeSerializer), GetType(WorkflowMarkupSerializer)), TypeConverter(GetType(ThemeTypeConverter))> _
    Public MustInherit Class DesignerTheme
        Implements IDisposable, IPropertyValueProvider
        ' Methods
        Protected Sub New(ByVal theme As WorkflowTheme)
            Me.workflowTheme = theme
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        End Sub

        Protected Overrides Sub Finalize()
            Try 
                Me.Dispose(False)
            Finally
                MyBase.Finalize
            End Try
        End Sub

        Friend Overridable Function GetPropertyValues(ByVal context As ITypeDescriptorContext) As ICollection
            Return New Object(0  - 1) {}
        End Function

        Public Overridable Sub Initialize()
        End Sub

        Public Overridable Sub OnAmbientPropertyChanged(ByVal ambientProperty As AmbientProperty)
        End Sub

        Private Sub System.IDisposable.Dispose() Implements IDisposable.Dispose
            Me.Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Private Function System.Workflow.ComponentModel.Design.IPropertyValueProvider.GetPropertyValues(ByVal context As ITypeDescriptorContext) As ICollection Implements IPropertyValueProvider.GetPropertyValues
            Return Me.GetPropertyValues(context)
        End Function


        ' Properties
        <Browsable(False)> _
        Public Overridable Property ApplyTo As String
            Get
                Return Me.designerTypeName
            End Get
            Set(ByVal value As String)
                If Me.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                Me.designerTypeName = value
            End Set
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
        Protected ReadOnly Property ContainingTheme As WorkflowTheme
            Get
                Return Me.workflowTheme
            End Get
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
        Public Overridable Property DesignerType As Type
            Get
                Return Me.designerType
            End Get
            Set(ByVal value As Type)
                If Me.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                Me.designerType = value
            End Set
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Property [ReadOnly] As Boolean
            Public Get
                Return ((Not Me.workflowTheme Is Nothing) AndAlso Me.workflowTheme.ReadOnly)
            End Get
            Friend Set(ByVal value As Boolean)
                If (Not Me.workflowTheme Is Nothing) Then
                    Me.workflowTheme.ReadOnly = value
                End If
            End Set
        End Property


        ' Fields
        Private designerType As Type
        Private designerTypeName As String = String.Empty
        Private workflowTheme As WorkflowTheme

        ' Nested Types
        Private Class ThemeSerializer
            Inherits WorkflowMarkupSerializer
            ' Methods
            Protected Overrides Function CreateInstance(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal type As Type) As Object
                If GetType(DesignerTheme).IsAssignableFrom(type) Then
                    Return Activator.CreateInstance(type, New Object() { serializationManager.Context.Item(GetType(WorkflowTheme)) })
                End If
                Return MyBase.CreateInstance(serializationManager, type)
            End Function

        End Class
    End Class
End Namespace

